<?php
// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.view');

class rcbaseViewMotor extends JView
{
	function display($tpl = null)
	{
		$task_hdl = array(
			'motor'     => 'motor_info',
			'addmotor'  => 'add_motor',
			'savemotor' => 'save_motor',
			'editmotor' => 'edit_motor',
			'delmotor'  => 'del_motor',
			'rateup'    => 'rate_up',
			'ratedown'  => 'rate_down',
		);
		$task = JRequest::getVar( 'task', 'motor_info' );
		$method = ($task_hdl[$task] ? $task_hdl[$task] : 'motor_info');
		$this->$method();
	}

	function motor_info($tpl = null){
		$model =& $this->getModel();
		$motor_id = JRequest::getInt( 'id', '1' );
		$motor = $model->getMotor($motor_id);
		$user =& JFactory::getUser();
		$motor_user =& JFactory::getUser($motor['user_id']);
		$data = array(
			'model' => $model,
			'motor' => $motor,
			'motor_id' => $motor_id,
			'user' => $user,
			'motor_user' => $motor_user,
		);
		$this->assignRef( 'data',	$data );

		$doc =& JFactory::getDocument();
		$doc->addScript('components/com_rcbase/js/jquery-ui/js/jquery-1.7.1.min.js');
		$doc->setTitle( 'База данных по ВМГ::МоторИнфо' );
		$mainframe =& JFactory::getApplication();
		$pathway   =& $mainframe->getPathway();
		$pathway->addItem('МоторИнфо', '/?option=com_rcbase&view=motor&task=motor&id='.$data['motor_id']);

		parent::setLayout('motor');
		parent::display($tpl);
	}

	function add_motor($tpl = null)
	{
		$user =& JFactory::getUser();

		$data = array(
			'user' => $user,
		);
		$this->assignRef( 'data',	$data );

		$doc =& JFactory::getDocument();
		$doc->setTitle( 'База данных по ВМГ' );
		$mainframe =& JFactory::getApplication();
		$pathway   =& $mainframe->getPathway();
		$pathway->addItem('Новый мотор', '');

		parent::setLayout('addmotor');
		parent::display($tpl);
	}

	function save_motor($tpl = null)
	{
		$user =& JFactory::getUser();
		$motor_id = JRequest::getInt('id');
		$model =& $this->getModel();
		if (($user->id and !$motor_id) or ($user->id == $motor_id)){
			$motor_id = $model->saveMotor();
		}
		$motor = $model->getMotor($motor_id);
		$data = array(
			'motor_id' => $motor_id,
			'model' => $model,
		);
		$this->assignRef( 'data', $data );

		$doc =& JFactory::getDocument();
		$doc->setTitle( 'База данных по ВМГ::МоторИнфо::Редактирование мотора' );
		$mainframe =& JFactory::getApplication();
		$pathway   =& $mainframe->getPathway();
		$pathway->addItem('МоторИнфо', '/?option=com_rcbase&view=motor&task=motor&id='.$data['motor_id']);
		$pathway->addItem('Редактирование мотора', '');

		parent::setLayout('savemotor');
		parent::display($tpl);
	}

	function del_motor($tpl = null)
	{
		$model =& $this->getModel();
		$motor_id = JRequest::getInt('id');
		$motor = $model->getMotor($motor_id);
		$user =& JFactory::getUser();
		if ($user->id == $motor['user_id']){
			$model->delMotor($motor_id);
		}
		$data = array(
			'motor_id' => $motor_id,
			'motor' => $motor,
			'model' => $model,
			'user' => $user,
		);
		$this->assignRef( 'data', $data );

		$doc =& JFactory::getDocument();
		$doc->setTitle( 'База данных по ВМГ::МоторИнфо::Редактирование мотора' );
		$mainframe =& JFactory::getApplication();
		$pathway   =& $mainframe->getPathway();
		$pathway->addItem('Редактирование мотора', '');

		parent::setLayout('delmotor');
		parent::display($tpl);
	}

	function edit_motor()
	{
		$model =& $this->getModel();
		$user =& JFactory::getUser();
		$motor_id = JRequest::getInt('id');
		$data = array(
			'user' => $user,
			'motor_id' => $motor_id,
			'model' => $model,
		);
		$this->assignRef( 'data', $data );

		$doc =& JFactory::getDocument();
		$doc->setTitle( 'База данных по ВМГ::МоторИнфо::Редактирование мотора' );
		$mainframe =& JFactory::getApplication();
		$pathway   =& $mainframe->getPathway();
		$pathway->addItem('МоторИнфо', '/?option=com_rcbase&view=motor&task=motor&id='.$data['motor_id']);
		$pathway->addItem('Редактирование мотора', '');

		parent::setLayout('editmotor');
		parent::display($tpl);
	}

	function rate_up(){
		$this->rate_motor('up');
	}

	function rate_down(){
		$this->rate_motor('down');
	}

	function rate_motor($voit)
	{
		$model =& $this->getModel();
		$user =& JFactory::getUser();
		$motor_id = JRequest::getInt('id');
		$voit_res = 'none';
		if ($user->guest){
			$voit_res = 'guest';
		} else {
			$voit_res = $model->setMotorRate($motor_id,$voit);
		}
		$data = array(
			'user'     => $user,
			'motor_id' => $motor_id,
			'model'    => $model,
			'viot'     => $voit,
			'voit_res' => $voit_res,
		);
		$this->assignRef( 'data', $data );

		$doc =& JFactory::getDocument();
		$doc->setTitle( 'База данных по ВМГ::МоторИнфо::Результат голосования' );
		$mainframe =& JFactory::getApplication();
		$pathway   =& $mainframe->getPathway();
		$pathway->addItem('МоторИнфо', '/?option=com_rcbase&view=motor&task=motor&id='.$data['motor_id']);
		$pathway->addItem('Результат голосования', '');

		parent::setLayout('ratemotor');
		parent::display($tpl);
	}
}